package com.homegoods.designer.ui.activity;

import android.graphics.Rect;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.designer.BuildConfig;
import com.homegoods.designer.R;
import com.homegoods.designer.databinding.DesignerHomeActivityBinding;
import com.homegoods.designer.model.CaseDetailModel;
import com.homegoods.designer.model.DesignerCaseModel;
import com.homegoods.designer.model.DesignerHomeModel;
import com.homegoods.designer.presenter.DesignerHomePresenter;
import com.homegoods.designer.ui.view.IDesignerHomeView;
import com.homegoods.designer.util.ShowIntroHelper;
import com.homegoods.aroutercomment.UserService;
import com.homegoods.aroutercomment.ARouterPath;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.base.MyBaseFragmentActivity;
import com.kedll.library.util.MyUtils;
import com.kedll.library.util.StringUtil;
import com.share.sharelibrary.share.ShareData;

import java.util.Random;

/**
 * @author QiQi
 * @description 设计师主页
 */
@Route(path = ARouterPath.PATH_DESIGNER_HOME)
public class DesignerHomeActivity extends MyBaseFragmentActivity<DesignerHomeActivityBinding, DesignerHomePresenter> implements IDesignerHomeView {

    @Autowired(name = "id")
    public String id;

    @Autowired(name = ARouterPath.PATH_USER_SERVICE)
    public UserService userService;

    private RecyclerView recyclerView;
    private DesignerHomeModel homeData;

    @Override
    public int getLayout() {
        return R.layout.designer_home_activity;
    }

    @Override
    protected void initEvent() {
        setTopRightButton("分享", R.mipmap.icon_share, v -> {
            new ShareData.Builder()
                    .setShareType(ShareData.SHARE_TEXT)
                    .setText("设计师")
                    .builder()
                    .startShare();
        });

        mPresenter.getAdapter().setOnItemClickListener((MultiTypeAdapter.OnItemClickListener<DesignerCaseModel>) (v, iItem) -> {
            DesignerCaseModel caseModel = iItem.getData();
            CaseDetailModel detailModel = new CaseDetailModel();
            detailModel.setCaseModel(caseModel);
            detailModel.setHomeModel(homeData);

            ARouter.getInstance()
                    .build(ARouterPath.PATH_CASE_DETAIL)
                    .withObject("data", detailModel)
                    .navigation();
        });

        mPresenter.loadData();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        mBinding.setPresenter(mPresenter);
        recyclerView = mBinding.recyclerView;

        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
//        recyclerView.setLayoutManager(MyUtils.getFlexBoxLayoutManager(this));
        recyclerView.addItemDecoration(new SpacesItemDecoration(2, 16));
        recyclerView.setAdapter(mPresenter.getAdapter());
    }

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        ARouter.getInstance().inject(this);
    }

    @Override
    protected DesignerHomePresenter getPresenter() {
        return new DesignerHomePresenter(this, id);
    }

    @Override
    public void setHomeData(DesignerHomeModel homeData) {
        this.homeData = homeData;
        mBinding.setItem(homeData);
        ShowIntroHelper introHelper = new ShowIntroHelper(mBinding.tvIntro, mBinding.tvMore);
        introHelper.setIntro(homeData.getIntro());

    }


    @Override
    public boolean isMine() {
        if (BuildConfig.IS_BUILD_MODULE) {
            return new Random().nextBoolean();
        } else {
            return StringUtil.isEqual(userService.getUserId(), id);
        }
    }

    public static class SpacesItemDecoration extends RecyclerView.ItemDecoration {

        private int space;
        private int itemCount;

        public SpacesItemDecoration(int itemCount, int space) {
            this.itemCount = itemCount;
            //屏幕宽度
            this.space = MyUtils.dp2px(space);

        }

        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            if (parent.getChildLayoutPosition(view) % itemCount == 0) {
                outRect.left = 0;
                outRect.right = space / 2;
            } else {
                outRect.right = 0;
                outRect.left = space / 2;
            }
        }
    }
}
